package com.lc.boxuegu.M_rightSideView;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author zhaiyj
 * @date 2025/6/18 20:58
 */
public class Solution {


    public List<Integer> rightSideView(TreeNode root) {

       List<Integer> ans = new ArrayList<>();
       dfs(root, 0, ans);
       return ans;
    }

    private void dfs(TreeNode node, int depth, List<Integer> ans){
        if (node == null){
            return;
        }
        if(depth == ans.size()){
            ans.add(node.val);
        }

        dfs(node.right, depth + 1, ans);
        dfs(node.left, depth + 1, ans);
    }


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }


}
